package com.central.apps.handler.MaterialInventory;

import cn.hutool.core.map.MapUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 出库管理
 */
@Component
public class matusetransHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        HashMap<String,Object> result=new HashMap<String, Object>() ;
        result.put("code",1);
        result.put("resp_msg","操作失败");
            //根据选择领料单，带出领料子表
            if("getLld".equals(type)){
                String lldnum= BLuntil.getParameter(params,"lldnum");//领料单编号
                String sql="select * from gq_itemapplyline where lldnum='"+lldnum+"'";
               return BLuntil.toPage(params,handlerService,sql);
            }
            // 出库按钮
            if("chuku".equals(type)){
                String id= BLuntil.getParameter(params,"id");//出库单 id
                List<Map<String, Object>> list = handlerService.findBySql("select * from gq_invuse where id ='"+id+"'");
                String location= MapUtil.getStr(list.get(0), "locations");//库房编号
                String lldnum= MapUtil.getStr(list.get(0), "lldnum");//领料单编号
                String invusenum= MapUtil.getStr(list.get(0), "invusenum");//出库单编号
                String status= MapUtil.getStr(list.get(0), "status");//出库单编号
                if("未出库".equals(status)) {

                    List<Map<String, Object>> linelist = handlerService.findBySql("select *  from gq_invuseline where invusenum = (select invusenum from gq_invuse where id ='" + id + "')");

                    if (linelist != null && linelist.size() > 0) {

                        // 更新领料单状态为已出库
                        handlerService.findBySql("update gq_itemapply set status='已出库' where LLDNUM='" + lldnum + "'");
                        // 更新出库单状态
                        handlerService.findBySql("update gq_invuse set status='已出库' where invusenum='" + invusenum + "'");
                        for (int k = 0; k < linelist.size(); k++) {
                            String itemNUm = MapUtil.getStr(linelist.get(k), "itemnum");//物资编码
                            Integer currentqty = MapUtil.getInt(linelist.get(k), "quantity");//领用数量
                            Integer innum = MapUtil.getInt(linelist.get(k), "grantqty");//出库数量
                            // 更新库存数量
                            handlerService.findBySql("update  bl_Inventorys  set innum=innum-" + currentqty + " ,actqty=actqty-" + innum + " where itemnum='" + itemNUm + "' and warehousenum ='" + location + "'");
                            result.put("code", 0);
                            result.put("resp_msg", "操作成功");
                            result.put("msg", "操作成功");

                        }
                    }
                }else{
                    result.put("code", 1);
                    result.put("resp_msg", "该单据已出库");
                    result.put("mcg", "该单据已出库");

                }

            }

        return result;
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        String location=BLuntil.getParameter(data,"LOCATIONS");
        String cknum=BLuntil.getParameter(data,"cknum");
        handlerService.findBySql("update gq_invuseline set LOCATION='"+location+"' where cknum='"+cknum+"' ");
        super.afterSave(app, objName, data, isUpdate);
    }
}
